package ac.man.cs.afzal.tcm.term_similarities;

import ac.man.cs.afzal.tcm.sql_handler.SQL_Handler_Comparisons_Impl;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Random;



/**
 * Title: Term Classifier
 *
 * Derive_Distinct_Sentences class converts the
 * <p>Company: The University of Manchester</p>
 *
 * @author Hammad Afzal
 * @version 1.0

 */
public class Parameter_Optimization
{

    private float alpha, beta, gamma, delta;
    private int noun_table, verb_table, pattern_table, lexical_table, matches;
    private SQL_Handler_Comparisons_Impl sqlHandler;
    String tables[] =
    {
        "Results_Final", "Results_Temp"
    };
    String file_contents = "";
    StringBuffer contents;

    public Parameter_Optimization()
    {
        sqlHandler = new SQL_Handler_Comparisons_Impl();
        sqlHandler.connect_db();

        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        String input;
        try
        {
            System.out.println("*****************************************************");
            System.out.print("WARNING: Your previous data in 'Results_Final' table and 'Results_Temp' \n " +
                    " tables will be lost \n" +
                    " Enter 'y' to continue or 'n' to exit : ");
            input = in.readLine();
            input = input.trim();

            if(input.equals("y"))
            {
                System.out.println("\n\n Enter Noun Table you want to use (Options are 1,2,3) : ");
                input = in.readLine();
                input = input.trim();
                noun_table = Integer.parseInt(input);
                System.out.println("\n\n Enter Verb Table you want to use (Options are 1,2,3) : ");
                input = in.readLine();
                input = input.trim();
                verb_table = Integer.parseInt(input);
                System.out.println("\n\n Enter Pattern Table you want to use (Options are 1,2,3) : ");
                input = in.readLine();
                input = input.trim();
                pattern_table = Integer.parseInt(input);
                System.out.println("\n\n Enter Lexical Table you want to use (Options are 1,2) : ");
                input = in.readLine();
                input = input.trim();
                lexical_table = Integer.parseInt(input);

            }
            else
            {
                System.out.println("Program Terminated .... ");
                System.exit(1);
            }
            in.close();
        }
        catch (IOException ex)
        {
            System.out.println("Exception while taking parameters input");
        }
        sqlHandler.get_test_terms();
        for(int i = 0; i < 25; i ++)
        {
            sqlHandler.delete_all(tables);
            sqlHandler.reset_auto_inc(tables);
            System.out.println("Database Reset");


            Random rand = new Random();
//          DecimalFormat df = new DecimalFormat("0.#####");
            beta = rand.nextFloat();
            beta = (float) (beta % 0.2);
            gamma = rand.nextFloat();
            gamma = (float) (gamma % 0.2);

            alpha = rand.nextFloat();
            alpha = (float) (alpha % 0.5);

            delta = 1 - alpha - beta - gamma;

//            beta = (float)0.0;
            //          gamma = (float)0.0;
            //        delta = (float)1.0;
            //      alpha = (float)0.0;

//            alpha = 1- delta - beta - gamma;
//            gamma = 1- delta - beta - alpha;
//            delta = 1- alpha - beta - gamma;
//            beta = 1- delta - alpha - gamma;

            sqlHandler.get_combined_results(alpha, beta, gamma, delta, noun_table, verb_table, pattern_table, lexical_table);
            matches = sqlHandler.get_matches();

            String[] relevance_value = new String[5];
            String[] doc_table1 =
            {
                "alpha", "beta", "gamma", "delta", "matches"
            };

            relevance_value[0] = Double.toString(alpha);
            relevance_value[1] = Double.toString(beta);
            relevance_value[2] = Double.toString(gamma);
            relevance_value[3] = Double.toString(delta);
            relevance_value[4] = Integer.toString(matches);
            sqlHandler.insert_table("parameters_1", doc_table1, relevance_value);
        }
    }
}
